##########################################################################################

library(dplyr)
library(ggplot2)
library(data.table)
library(RColorBrewer)
library(optparse)

##########################################################################################

option_list <- list(
    make_option(c("--maf_file"), type = "character") ,
    make_option(c("--images_path"), type = "character") ,
    make_option(c("--info_file"), type = "character")
)

if(1!=1){
    
    work_dir <- "~/20220915_gastric_multiple/dna_combine/"
    maf_file <- paste(work_dir,"/maf/All_GGA.all.maf",sep="")
	images_path <- paste(work_dir,"/images/mutRate",sep="")
	info_file <- paste(work_dir,"/config/tumor_normal.class.list",sep="")

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

maf_file <- opt$maf_file
info_file <- opt$info_file
images_path <- opt$images_path

dir.create(images_path , recursive = T)

###########################################################################################

info <- data.frame(fread(info_file))
dat_maf <- data.frame(fread( maf_file ))

###########################################################################################

Variant_Type <- c("Missense_Mutation","Nonsense_Mutation","Frame_Shift_Ins","Frame_Shift_Del","In_Frame_Ins","In_Frame_Del","Splice_Site","Nonstop_Mutation")

###########################################################################################

maf <- dat_maf[dat_maf$t_alt_count>0,]
maf_use <- data.frame(
    Hugo_Symbol = maf$Hugo_Symbol, 
    Chromosome = maf$Chromosome , Start_Position =  maf$Start_Position , End_Position = maf$End_Position ,
    Reference_Allele = maf$Reference_Allele , Tumor_Seq_Allele2 = maf$Tumor_Seq_Allele2 , 
    Variant_Classification = maf$Variant_Classification , Tumor = maf$Tumor_Sample_Barcode  )

maf_use <- subset( maf_use , Variant_Classification %in% Variant_Type )
maf_use <- merge( maf_use , info , by = "Tumor" )

###########################################################################################
## 判断不同来源的IM
maf_use$Class_combine <- "IM"
maf_use$Class_combine[grep( "GC" , maf_use$Class)] <- "GC"
maf_use$Class_combine <- ifelse( maf_use$Class_combine == "IM" & maf_use$Type == "IM + IGC" , "IM(IGC)" , maf_use$Class_combine)
maf_use$Class_combine <- ifelse( maf_use$Class_combine == "IM" & maf_use$Type == "IM + DGC" , "IM(DGC)" , maf_use$Class_combine)
maf_use$Class_combine <- ifelse( maf_use$Class_combine == "IM" & maf_use$Type == "IM + IGC + DGC" , "IM(IGC_DGC)" , maf_use$Class_combine)

info$Class_combine <- "IM"
info$Class_combine[grep( "GC" , info$Class)] <- "GC"
info$Class_combine <- ifelse( info$Class_combine == "IM" & info$Type == "IM + IGC" , "IM(IGC)" , info$Class_combine)
info$Class_combine <- ifelse( info$Class_combine == "IM" & info$Type == "IM + DGC" , "IM(DGC)" , info$Class_combine)
info$Class_combine <- ifelse( info$Class_combine == "IM" & info$Type == "IM + IGC + DGC" , "IM(IGC_DGC)" , info$Class_combine)

mut_rate <- maf_use %>%
group_by( Hugo_Symbol , Class_combine ) %>%
summarize( MutNum = length(unique(ID )) )

class_num <- info %>%
group_by( Class_combine) %>%
summarize( SampleNum = length(unique(ID)) )
mut_rate <- merge( mut_rate , class_num , by = "Class_combine" )
mut_rate$MutRate <- mut_rate$MutNum/mut_rate$SampleNum
mut_rate_combine <- mut_rate

mut_rate_combine <- subset(mut_rate_combine , Class_combine!="GC")
colnames(mut_rate_combine)[1] <- "Class"

out_name <- paste0( images_path , "/MutRate.IM.tsv" )
write.table( mut_rate_combine , out_name , row.names = F , quote = F , sep = "\t" )

###########################################################################################
## 判断不同Type
## 以人为单位
mut_rate <- maf_use %>%
group_by( Hugo_Symbol , Class , Type ) %>%
summarize( MutNum = length(unique(ID )) )

class_num <- info %>%
group_by( Type) %>%
summarize( SampleNum = length(unique(ID)) )
mut_rate <- merge( mut_rate , class_num , by = "Type" )
## 分IGC、DGC和IM
mut_rate$MutRate <- mut_rate$MutNum/mut_rate$SampleNum

out_name <- paste0( images_path , "/MutRate.Type.tsv" )
write.table( mut_rate , out_name , row.names = F , quote = F , sep = "\t" )
